//
//  MyUtilities.cpp
//  Dots
//
//  Created by hoangtx on 5/29/13.
//
//

#include "MyUtilities.h"

#include "StaticVariables.h"

double MyUtilities::triTuyetDoi(double _value){
    if(_value >=0){
        return _value;
    }else{
        return -_value;
    }
}


double MyUtilities::distanceOfTwoPoints(cocos2d::CCPoint pointA, cocos2d::CCPoint pointB){
    return sqrt((pointB.x - pointA.x)*(pointB.x - pointA.x) + (pointB.y - pointA.y)*(pointB.y - pointA.y));
}


char* MyUtilities::toString(int _value){
    
    char* tempString = new char[20];
    sprintf(tempString, "%i",_value);
    return tempString;
}

CCPoint MyUtilities::getCenterPointOfSizeInsideParentRect(cocos2d::CCRect parentRect, cocos2d::CCSize childSize){
    CCPoint resultPoint = ccp(parentRect.origin.x+(parentRect.size.width - childSize.width)/2 , parentRect.origin.y+(parentRect.size.height - childSize.height)/2);
    return resultPoint;
}

void MyUtilities::drawRect(cocos2d::CCRect rectToDraw){
    ccDrawRect(rectToDraw.origin, ccp(rectToDraw.getMaxX(),rectToDraw.getMaxY()));
}

void MyUtilities::drawRectAndFillWithColor(CCRect rectToDraw,ccColor4F color){
    ccDrawSolidRect(rectToDraw.origin, ccp(rectToDraw.getMaxX(),rectToDraw.getMaxY()), color);
}

void MyUtilities::drawRectAndFillWithColor(CCRect rectToDraw,ccColor4B color){
    ccDrawSolidRect(rectToDraw.origin, ccp(rectToDraw.getMaxX(),rectToDraw.getMaxY()), ccc4FFromccc4B(color));
}

CCRect MyUtilities::getRectOfNode(cocos2d::CCNode *node){
    return CCRectMake(node->getPositionX(), node->getPositionY(), node->getContentSize().width, node->getContentSize().height);
}


CCPoint MyUtilities::getCoCosPointFromTouches(cocos2d::CCSet *touches){
    CCTouch* touch = (CCTouch*)touches->anyObject();
    CCPoint location = touch->getLocationInView();
    CCPoint cocoLocation = CCDirector::sharedDirector()->convertToGL(location);

    return cocoLocation;
}





long MyUtilities::milisecondsNow(){
    struct cc_timeval now;
    CCTime::gettimeofdayCocos2d(&now, NULL);
    return (now.tv_sec * 1000 + now.tv_usec / 1000);
}


